package com.museum.core.bean;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.museum.base.BaseApiService;
import com.museum.core.utils.RedisUtil;


import java.text.DateFormat;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: 王瑞文
 * Date: 2020/9/16
 * Time: 16:34
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Component
public class LoginInterceptor extends BaseApiService<JSONObject> implements HandlerInterceptor {

    @Resource
    private RedisUtil redisUtil;

    // 目标方法执行之前
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println(Thread.currentThread().getName() + "线程执行了此请求");
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = dateFormat.format(new Date());


        if (request.getRequestURI().equals("/user/loginById")) {
            return true;
        }
        String token = request.getHeader("Authorization");
        if (StringUtils.isEmpty(token)) {
            System.out.println("WEB请求" + "URL:" + request.getRequestURL() + "请求被拦截 无Token " + "date:" + date);
            String error = JSON.toJSONString(setResultError("请登录！" + "date:" + date));
            response.setStatus(401);
            returnJson(response, error);
            return false;
        } else {

            if (!StringUtils.isEmpty(redisUtil.getString(token))) {
                System.out.println("WEB请求" + "URL:" + request.getRequestURL() + "请求被放行" + "date:" + date);
                return true;
            } else {
                System.out.println("WEB请求" + "URL:" + request.getRequestURL() + "Token失效！" + "date:" + date);
                String error = JSON.toJSONString(setResultError("登陆信息过期！"));
                response.setStatus(401);
                returnJson(response, error);
                return false;
            }
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }

    public void returnJson(HttpServletResponse response, String result) throws Exception {
        PrintWriter writer = null;
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        try {
            writer = response.getWriter();
            writer.println(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

